/*******************************************************************************
 * Copyright (c) 2014 Steffen Mikkelsen
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Steffen Mikkelsen - initial API and implementation
 ******************************************************************************/
package com.subsecret.secureconnection.securestream.layers.helper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.subsecret.secureconnection.securestream.encryption.DiffieHellmanClient;
import com.subsecret.secureconnection.securestream.encryption.DiffieHellmanServer;
import com.subsecret.secureconnection.streamtools.StreamTools;

public class DiffieHellmanGenerator {
    
    public static byte[] generateKeyServer(InputStream in, OutputStream out) throws IOException {
        DiffieHellmanClient client = new DiffieHellmanClient();
        byte[] recv = StreamTools.receiveByteArray(in);        
        byte[] toServer = client.generateClientPart(recv);
        StreamTools.sendByteArray(out, toServer);        
        return client.getSharedSecret();
    }
    
    public static byte[] generateKeyClient(InputStream in, OutputStream out) throws IOException {
        DiffieHellmanServer server = new DiffieHellmanServer();
        byte[] part1 = server.generateServerPart1();
        StreamTools.sendByteArray(out, part1);
        byte[] recv = StreamTools.receiveByteArray(in);
        server.generateServerPart2(recv);
        return server.getSharedSecret();
    }
}
